package com.weimei.conversionTreasure.viewModel;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.activity.VipActivity;
import com.weimei.conversionTreasure.adapter.CBAdapter;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.SheRuBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class CBVM extends BaseViewModel<Repository> {

    public CBVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public ObservableField<String> alcoholWeight = new ObservableField<>();

    public ObservableField<String> result = new ObservableField<>();
    public ObservableField<String> result1 = new ObservableField<>();

    RecyclerView mCbRecy;
    CBAdapter cbAdapter;
    int num = 1;
    private List<CBAdapter.CBBean> list = new ArrayList<>();

    public void setView(RecyclerView cbRecy) {
        mCbRecy = cbRecy;
        cbAdapter = new CBAdapter();
        mCbRecy.setAdapter(cbAdapter);
        for (int i = 0; i < num; i++) {
            CBAdapter.CBBean cbBean = new CBAdapter.CBBean();
            list.add(cbBean);
        }
        cbAdapter.setList(list);
    }

    public BindingCommand add = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            CBAdapter.CBBean cbBean = new CBAdapter.CBBean();
            cbAdapter.addData(cbBean);
        }
    });

    public BindingCommand remove = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (cbAdapter.getData().size() > 1) {
                cbAdapter.getViewByPosition(cbAdapter.getData().size()-1, R.id.edt_cp_unit).clearFocus();
                cbAdapter.getViewByPosition(cbAdapter.getData().size()-1, R.id.edt_cp_use).clearFocus();
                cbAdapter.getViewByPosition(cbAdapter.getData().size()-1, R.id.edt_cp_cb).clearFocus();

                CBAdapter.CBBean cbBean = cbAdapter.getData().get(cbAdapter.getData().size()-1);
                cbAdapter.remove(cbBean);
            }

        }
    });

    public BindingCommand js=  new BindingCommand(() -> {
        if (UserUtils.getFreeNum() > 0){
            cbjs();
        }else {
            if (UserUtils.isVip()){
                cbjs();
            }else {
                ToastUtils.showShort("您还不是会员，请先开通会员后再使用！！！");
                startActivity(VipActivity.class);
            }
        }


    });

    private void cbjs() {
        if (ValidateUtils.isEmptyString(alcoholWeight.get())){
            ToastUtils.showShort("请输入酒度");
            return;
        }

        for (CBAdapter.CBBean bean : cbAdapter.getData()) {
            if (ValidateUtils.isEmptyString(bean.getWineUnitPrice())){
                ToastUtils.showShort("请输入产品信息");
                return;
            }
        }
        String wineJson = new Gson().toJson(cbAdapter.getData());
        Log.d("XXXX",wineJson);



        Map<String,String> map = new HashMap<>();
        map.put("alcoholWeight",alcoholWeight.get());
        map.put("wineJson",wineJson);
        model.costing(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<SheRuBean>() {
                    @Override
                    public void onNext(SheRuBean sheRuBean) {
                        if (sheRuBean.issucc){
                            result.set(sheRuBean.getData().getCostPerJin()+"");
                            result1.set(sheRuBean.getData().getTotalCost()+"");
                            UserUtils.opreFree();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
